package cohesion.net
{
	import flash.events.EventDispatcher;
	import flash.utils.getTimer;
	
	[Event(name="progress", type="flash.events.ProgressEvent")]
	[Event(name="complete", type="flash.events.Event")]
	[Event(name="ioError", type="flash.events.IOErrorEvent")]
	[Event(name="securityError", type="flash.events.SecurityErrorEvent")]
	
	/**
	 * 加载项
	 * @author 徐俊杰
	 * @date 2012-2-22
	 */
	public class LoadItem extends EventDispatcher
	{
		private var _loader:*;
		
		private var _status:int;
		
		private var _startTime:Number;
		
		private var _completeTime:Number;
		
		private var _tringNumber:uint;
		
		private var _maxTries:uint;
		
		private var _key:String;
		
		private var _url:String;
	
		/**
		 *  实例化加载项 
		 * 
		 */		
		public function LoadItem()
		{
		
			_status = LoadItemStatus.WAIT;
		}
		
		/**
		 * 加载器 
		 */
		public function get loader():*
		{
			return _loader;
		}

		/**
		 * @private
		 */
		public function set loader(value:*):void
		{
			_loader = value;
		}

		/**
		 * 加载状态，参考LoadItemStatus类 
		 * @see LoadItemStatus
		 */
		public function get status():int
		{
			return _status;
		}

		/**
		 * @private
		 */
		public function set status(value:int):void
		{
			_status = value;
		}

		/**
		 *  已消耗的加载时间
		 */
		public function get time():Number
		{
			if(_status == LoadItemStatus.WAIT)
				return 0;
			else if(_status == LoadItemStatus.LOADED)
				return _completeTime - _startTime;
			return getTimer() - _startTime;
		}

		/**
		 *  已经尝试过加载的次数，每次开始加载时，此值将加1
		 *  第一次加载开始后，此值为1
		 */
		public function get triedNumber():uint
		{
			return _tringNumber;
		}

		/**
		 * @private
		 */
		public function set triedNumber(value:uint):void
		{
			_tringNumber = value;
		}

		/**
		 *  尝试加载的最大次数
		 *  尝试的数次超过此值，仍然未加载成功的项，将停止加载
		 */
		public function get maxTries():uint
		{
			return _maxTries;
		}

		/**
		 * @private
		 */
		public function set maxTries(value:uint):void
		{
			_maxTries = value;
		}

		/**
		 * 标识符 
		 */
		public function get key():String
		{
			if(_key != null)
				return _key;
			else
				return _url;
		}

		/**
		 * @private
		 */
		public function set key(value:String):void
		{
			_key = value;
		}

		/**
		 * 路径 
		 */
		public function get url():String
		{
			return _url;
		}

		/**
		 * @private
		 */
		public function set url(value:String):void
		{
			_url = value;
		}

		/**
		 * 开始加载时间 
		 */
		public function get startTime():Number
		{
			return _startTime;
		}

		/**
		 * @private
		 */
		public function set startTime(value:Number):void
		{
			_startTime = value;
		}

		/**
		 * 加载完成时间 
		 */
		public function get completeTime():Number
		{
			return _completeTime;
		}

		/**
		 * @private
		 */
		public function set completeTime(value:Number):void
		{
			_completeTime = value;
		}

		
	}
}